package kr.saeildamil.androidstudy.ch19;

import kr.saeildamil.androiddforstudy.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class ThreadTest extends Activity {
	
	int mMainValue;
	int mBackValue;
	
	TextView mMain;
	TextView mBack;
	
	BackThread thread;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_threadtest);
		
		mMain = (TextView) findViewById(R.id.ch19_threadtest_maintv);
		mBack = (TextView) findViewById(R.id.ch19_threadtest_backtv);
		
		thread = new BackThread(mHandler);
		thread.setDaemon(true);
		thread.start();
		
	} // End of method onCreate;
	
	public void mOnClick(View v){
		mMainValue ++;
		mMain.setText("Main Value : " + mMainValue);
	}
	
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if(msg.what==0){
				mBack.setText("Back Value : " + mBackValue);
			}
		}
	};
}
	class BackThread extends Thread{
			int mBackValue = 0;
			Handler mHandler;
			
		 BackThread(Handler handler) {
			 mHandler = handler;
		}

		@Override
		public void run() {
			
			while (true) {
				mBackValue ++;
				Message msg = new Message();
				msg.what = 0;
				msg.arg1 = mBackValue;
				mHandler.sendMessage(msg);
				try {
					Thread.sleep(1000);
					// mBack.setText("Back Value : " + mBackValue);
				} catch (InterruptedException e) {
				}
			}
		}
		
	} // End of Class BackThread;
	
